home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5592 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  37 lines

  1. Path: pegasus.montclair.edu!harmon
  2. From: harmon@pegasus.montclair.edu (Derek Harmon)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: truncation function?
  5. Date: 15 Feb 1996 00:47:57 -0500
  6. Organization: Montclair State University
  7. Message-ID: <harmon.824362384@pegasus.montclair.edu>
  8. References: <4fjc0k$nev$1@mhadg.production.compuserve.com>
  9. NNTP-Posting-Host: pegasus.montclair.edu
  10. X-Newsreader: NN version 6.5.0 #68 (NOV)
  11.  
  12. Steve Eckmann <71055.1153@CompuServe.COM> writes:
  13.  
  14. >I need a truncation function "float trunc(float in; int 
  15. >precision)" that
  16. >takes a float and a precision spec and returns the input float 
  17. >truncated to
  18. >"precision" decimal digits. Similarly for "round". This seems so 
  19. >simple
  20.  
  21. #include <math.h>
  22.  
  23. float trunc(float in, float epsilon)
  24. {
  25.     if (in > (0.0 + epsilon)) return( (float) floor(in) );
  26.     if (in < (0.0 - epsilon)) return( (float) ceil(in) );
  27.     return( 0.0 );
  28. }
  29.   
  30.                                                       -- Stone
  31. --
  32. # Derek Harmon (aka Stonelight)    harmon@pegasus.montclair.edu
  33. # - Computer Science Undergrad, Montclair State University, NJ
  34. # - My views are my own, nobody else is this creative.  3;)>
  35. ... 640K ought to be enough for anyone - Bill Gates '81
  36.  
  37.